package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.ReasonAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.ReasonBen;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.preference.PreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class ReasonActivity extends AppCompatActivity {

    //@Bind(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
  //  @Bind(R.id.toolbar)
    Toolbar mToolbar;
   // @Bind(R.id.reasonListView)
    ListView mReasonListView;
    List mReasonList = null;
    int reasonType = 0;
    int deviceType = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reason);

        mTvToolbarTitle=(TextView)findViewById(R.id.tv_toolbar_title);
        mToolbar=(Toolbar)findViewById(R.id.toolbar);
        mReasonListView=(ListView)findViewById(R.id.reasonListView);
        mTvToolbarTitle.setText("原因");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        reasonType = Integer.parseInt(getIntent().getStringExtra("reasonType"));
        String deviceTypeStr =getIntent().getStringExtra("deviceType");
        if(deviceTypeStr!=null)
        {
            deviceType = Integer.parseInt(deviceTypeStr);
        }

        if(reasonType==0)
        {
//            List<ReasonBen> reasonList= PreferencesUtil.getRepairReasonList(ReasonActivity.this);
//            if(reasonList==null)
//            {
                requestRepairReasonList();
//            }else {
//                getReasonFromType(deviceType,reasonList);
//            }
        }else if(reasonType==1)
        {
          //  List<ReasonBen> reasonList= PreferencesUtil.getScrapReasonList(ReasonActivity.this);
          //  if(reasonList==null) {
                requestScrapReasonList();
//            }else
//            {
//                getReasonFromType(deviceType,reasonList);
//            }
        }else
        {
//            List<ReasonBen> reasonList= PreferencesUtil.getKeepReasonList(ReasonActivity.this);
//            if(reasonList==null) {
                requestKeepReasonList();
//            }else
//            {
//                getReasonFromType(deviceType,reasonList);
//            }
        }

    }
    private void getReasonFromType(int type,List<ReasonBen> reasonList)
    {
        ArrayList list = new ArrayList();
        if(type!=-1) {
            for (ReasonBen item : reasonList) {
                Log.e("____________________", item.getName()+"_______________________________" );
                Log.e("____________________", String.valueOf(item.getDevice_type())+"_______________________________" );
                if (item.getDevice_type() == type) {
                    Log.e("____________________", "type" + item.getName() + "_______________________________");
                    list.add(item);
                }
            }
            updateReasonList(list);
        }else
        {
            Log.e("____________________","_______________________________" );
            updateReasonList(reasonList);
        }
    }

    private void requestRepairReasonList() {
        HttpClient client = new HttpClient(ReasonActivity.this);
        Call call = client.service().getAllRepairReason();
        client.request(call, new ResponseHandler<List<ReasonBen>>() {
            @Override
            public void onSuccess(List<ReasonBen> reasonBenList) {
                Log.e("____________________", "requestRepairReasonList: "+"_______________________________" );
                PreferencesUtil.saveRepairReasonList(ReasonActivity.this, reasonBenList);
                getReasonFromType(deviceType,reasonBenList);
            }

            @Override
            public void onFailure(int code, Error e) {

            }
        });
    }
    private void requestScrapReasonList() {
        HttpClient client = new HttpClient(ReasonActivity.this);
        Call call = client.service().getAllScrapReason();
        client.request(call, new ResponseHandler<List<ReasonBen>>() {
            @Override
            public void onSuccess(List<ReasonBen> reasonBenList) {
                Log.e("____________________", "requestScrapReasonList: "+"_______________________________" );
                PreferencesUtil.saveScrapReasonList(ReasonActivity.this, reasonBenList);
                getReasonFromType(deviceType,reasonBenList);
            }
            @Override
            public void onFailure(int code, Error e) {

            }
        });
    }

    private void requestKeepReasonList() {
        HttpClient client = new HttpClient(ReasonActivity.this);
        Call call = client.service().keepReasonList();
        client.request(call, new ResponseHandler<List<ReasonBen>>() {
            @Override
            public void onSuccess(List<ReasonBen> reasonBenList) {
                PreferencesUtil.saveKeepReasonList(ReasonActivity.this, reasonBenList);
                getReasonFromType(deviceType,reasonBenList);
            }
            @Override
            public void onFailure(int code, Error e) {

            }
        });
    }

    private void updateReasonList(List dataSource) {
        mReasonList = new ArrayList<>();
        mReasonList.addAll(dataSource);
        mReasonListView.setAdapter(new ReasonAdapter(this,mReasonList));
        mReasonListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                ReasonBen item = (ReasonBen) mReasonList.get(position);
                Intent intent = new Intent(ReasonActivity.this, SubReasonActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("subReason",item);
                mBundle.putInt("reasonType",reasonType);
                intent.putExtras(mBundle);
                startActivity(intent);
            }
        });
    }

}
